<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">年份</label>
                            <div class="layui-input-inline">
                                <input type="text" name="year" autocomplete="off" class="layui-input" id="year">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">季度</label>
                            <div class="layui-input-inline">
                                <select name="quarter" lay-verify="" id="quarter">
                                    <option value="0">第一季度</option>
                                    <option value="1">第二季度</option>
                                    <option value="2">第三季度</option>
                                    <option value="3">第四季度</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-inline">
                                <select name="type" lay-verify="" id="type">
                                    <option value="0">个人</option>
                                    <option value="1">家庭</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <div class="layui-row layui-col-space15" style="margin-top: 10px">
            <div class="layui-col-md6">
                <div class="layui-card">
                    <div class="layui-card-header"><i class="fa fa-line-chart icon"></i>当前季度</div>
                    <div class="layui-card-body">
                        <div id="this-quarter" style="width: 100%;min-height:500px"></div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-card">
                    <div class="layui-card-header"><i class="fa fa-line-chart icon"></i>与上季度对比</div>
                    <div class="layui-card-body">
                        <div id="contrast" style="width: 100%;min-height:500px"></div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script>
    layui.use(['form', 'laydate','miniPage', 'axios','echarts','layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            echarts = layui.echarts,
            layer = layui.layer,
            axios = layui.axios,
            miniPage = layui.miniPage;

        laydate.render({
            elem: '#year',
            type: 'year',
            value: new Date()
        })
        form.render(null, 'search');


        //渲染季度的情况
        var chartDom = document.getElementById('this-quarter');
        var quarter = echarts.init(chartDom);
        var option;
        option = {
            title: {
                text: '本季度统计',
                // subtext: '纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
            },
            series: [
                {
                    name: '本季度统计',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        {value: 1048, name: '支出'},
                        {value: 735, name: '收入'},
                        {value: 580, name: '投资'},
                        {value: 484, name: '借贷'}
                    ],
                    itemStyle: {
                        normal: {
                            label: {
                                show: true,
                                formatter: '{b}:{c}￥ ({d}%)'
                            }
                        }

                    },
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            ]
        };
        option && quarter.setOption(option);

        //和上季度对比
        var contrast = echarts.init(document.getElementById('contrast'), 'walden');
        option = {
            title: {
                text: "与上季度对比"
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['本季度', '上季度']
            },
            toolbox: {
                show: true,
                feature: {
                    // dataView: {show: true, readOnly: false},
                    // magicType: {show: true, type: ['line', 'bar']},
                    // restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            calculable: true,
            xAxis: [
                {
                    type: 'category',
                    data: ['支出', '收入', '投资', '借贷']
                }
            ],
            yAxis: [
                {
                    name: '金额',
                    type: 'value',
                    axisLabel: {
                        formatter: '{value} ￥'
                    }
                }
            ],
            series: [
                {
                    name: '本季度',
                    type: 'bar',
                    // data: [2.0, 4.9, 7.0, 23.2],
                    markPoint: {
                        data: [
                            {type: 'max', name: '本季度最大值'},
                            {type: 'min', name: '本季度最小值'}
                        ]
                    }
                },
                {
                    name: '上季度',
                    type: 'bar',
                    // data: [2.6, 5.9, 9.0, 26.4],
                    markPoint: {
                        data: [
                            {name: '上季度最大值', type: 'max'},
                            {name: '上季度最小值', type: 'min'}
                        ]
                    }
                }
            ]
        };
        contrast.setOption(option);

        //data包含三个属性：year(查询的年份), quarter（查询的季度）,type(查询的类型，0代表个人， 1代表家庭)
        function init(data){
            //请求当前季度的统计
            axios({
                url: "statistic/quarterData",
                method: 'post',
                data: data
            }).then(response => {
                console.log(response)
                if(response.code == 0){
                    let newOption = {
                        series: [
                            {
                                data: response.data
                            }
                        ]
                    }
                    quarter.setOption(newOption);
                }
            }).catch()

            //请求本季度与上季度对比的数据统计
            // data.year = new Date().getFullYear();
            axios({
                url: 'statistic/quarterContrast',
                method: 'post',
                data: data
            }).then(response => {
                if (response.code == 0){
                    let newOption = {
                        series: [
                            {
                                name: '本季度',
                                type: 'bar',
                                data: response.data.thisQuarter,
                                markPoint: {
                                    data: [
                                        {type: 'max', name: '本季度最大值'},
                                        {type: 'min', name: '本季度最小值'}
                                    ]
                                }
                            },
                            {
                                name: '上季度',
                                type: 'bar',
                                data: response.data.lastQuarter,
                                markPoint: {
                                    data: [
                                        {name: '上季度最大值', type: 'max'},
                                        {name: '上季度最小值', type: 'min'}
                                    ]
                                }
                            }
                        ]
                    }
                    contrast.setOption(newOption);
                }else{
                    layer.msg("加载失败");
                }
            }).catch(error => {
            })
        }

        let date = new Date();
        let quarterValue = 0;
        if (date.getMonth() < 3){
            quarterValue = 0;
        }else if (date.getMonth() < 6){
            quarterValue = 1;
        }else if (date.getMonth() < 9){
            quarterValue = 2
        }else if (date.getMonth() < 12){
            quarterValue = 3;
        }
        let initData = {
            year : date.getFullYear(),
            quarter: quarterValue,
            type: 0
        }
        form.val('search',{
            year: initData.year,
            quarter: initData.quarter,
            type: initData.type
        })
        form.render(null, 'search');

        init(initData);
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            init(data.field)
            return false;
        });
    });
</script>